﻿using System.ComponentModel;
using System.Web.UI;

namespace Besture.CmsControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
    public class TextBox : System.Web.UI.WebControls.TextBox
    {
        /// <summary>
        /// 初始化控件，设置默认值
        /// </summary>
        public TextBox()
        {
            Required = false;
            CssClass = "input-text";
        }
        [Bindable(true)]
        [DefaultValue("False")]
        [Localizable(true)]
        [Category("Action")]
        [Description("输入验证")]
        public bool Required
        {
            get
            {
                bool s = (bool)ViewState["Required"];
                //添加jquery.validate验证
                if (s)
                {
                    this.CssClass += " required";
                }
                return s;
            }
            set
            {
                ViewState["Required"] = value;
            }
        }

        /// <summary>
        /// 宽度
        /// </summary>
        [Category("Appearance")]
        public Width WidthExt
        {
            get { return (Width)ViewState["WidthExt"]; }
            set
            {
                ViewState["WidthExt"] = value;
                SetWidth();
            }
        }

        /// <summary>
        /// 验证
        /// </summary>
        [Category("Data")]
        public Validate ValidateExt
        {
            get { return (Validate)ViewState["Validate"]; }
            set
            {
                ViewState["Validate"] = value;
                SetValidate();
            }
        }

        /// <summary>
        /// 提示
        /// </summary>
        [Category("Data")]
        public string Tips
        {
            get { return (string)ViewState["Tips"]; }
            set
            {
                ViewState["Tips"] = value;
            }
        }

        /// <summary>
        /// 设置控件宽度
        /// </summary>
        private void SetWidth()
        {
            switch (WidthExt)
            {
                case CmsControl.Width.Width_50:
                    this.Width = 50;
                    break;

                case CmsControl.Width.Width_100:
                    this.Width = 100;
                    break;

                case CmsControl.Width.Width_150:
                    this.Width = 150;
                    break;

                case CmsControl.Width.Width_200:
                    this.Width = 200;
                    break;

                case CmsControl.Width.Width_250:
                    this.Width = 250;
                    break;

                case CmsControl.Width.Width_300:
                    this.Width = 300;
                    break;

                case CmsControl.Width.Width_350:
                    this.Width = 350;
                    break;

                case CmsControl.Width.Width_400:
                    this.Width = 400;
                    break;
            }
        }

        /// <summary>
        /// 设置验证
        /// </summary>
        private void SetValidate()
        {
            switch (ValidateExt)
            {
                case CmsControl.Validate.Email:
                    this.CssClass += " email";
                    break;
                case CmsControl.Validate.Url:
                    this.CssClass += " url";
                    break;
                //日期控件 
                case CmsControl.Validate.Date:
                    this.CssClass += "  Wdate";
                    break;

                case CmsControl.Validate.DateISO:
                    this.CssClass += " dateISO";
                    break;
                case CmsControl.Validate.Number:
                    this.CssClass += " number";
                    break;
                case CmsControl.Validate.Creditcard:
                    this.CssClass += " creditcard";
                    break;
                case CmsControl.Validate.IP4:
                    this.CssClass += " ip4";
                    break;
                case CmsControl.Validate.Mobile:
                    this.CssClass += " mobile";
                    break;
                case CmsControl.Validate.Zipcode:
                    this.CssClass += " zipcode";
                    break;
                case CmsControl.Validate.QQ:
                    this.CssClass += " qq";
                    break;
                case CmsControl.Validate.IDCard:
                    this.CssClass += " idcard";
                    break;
                case CmsControl.Validate.Tel:
                    this.CssClass += " tel";
                    break;
                case CmsControl.Validate.English:
                    this.CssClass += " english";
                    break;
                case CmsControl.Validate.EnNum:
                    this.CssClass += " en_num";
                    break;
                case CmsControl.Validate.Digits:
                    this.CssClass += " digits";
                    break;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //文本框必输情况对应图片
            var img = Required ? "<img src='/Admin/Themes/Images/indispensable.gif' align='absMiddle' />" : "<img src='/Admin/Themes/Images/dispensable.png' align='absMiddle' />";
            writer.Write(img);
            base.Render(writer);
            //输出文本框相应提示
            if (!string.IsNullOrEmpty(Tips))
            {
                string tipsStr = string.Format(@"<span class=""notes"">※注：{0}</span>", Tips);
                writer.Write(tipsStr);
            }
        }
    }
}


//Category相关属性
//外观（Appearance）
//行为 （Behavior）
//布局（Layout）
//数据（Data）
//操作（Action）
//键盘（Key）
//和鼠标（Mouse）
//除此之外，还可以自定义 分组类别。